#include "stdafx.h"
#include "miiPin.h"
#include "..\..\include\imp_typedefine.h"
#include "..\..\include\imp_io_wrapper.h"
#include "..\..\include\imp_timer_wrapper.h"

CMiiShowPinIn::CMiiShowPinIn(CBaseFilter *pFilter, CCritSec *pLock, HRESULT *phr) : CBaseInputPin(NAME("CPinIn"), pFilter, pLock, phr, L"MiiShow input")
{
	ZeroMemory(&m_tType, sizeof(m_tType));
	mOutWidth  = 176;
	mOutHeight = 144;
}

CMiiShowPinIn::~CMiiShowPinIn()
{
}

HRESULT CMiiShowPinIn::CompleteConnect(IPin *pReceivePin)
{
	return S_OK;
}

HRESULT CMiiShowPinIn::SetMediaType(const CMediaType *p)
{
	if(NULL != p->pbFormat)
	{
		memcpy(&m_vInfo, p->pbFormat, sizeof(VIDEOINFOHEADER));
		mOutWidth = m_vInfo.bmiHeader.biWidth;
		mOutHeight = m_vInfo.bmiHeader.biHeight;
		return S_OK;
	}
	return S_FALSE;
}

HRESULT CMiiShowPinIn::CheckConnect(IPin *)
{
	return S_OK;
}


BOOL CMiiShowPinIn::IsMatch(const CMediaType* pmt1, const CMediaType* pmt2)
{
	if (pmt1->majortype != pmt2->majortype) return FALSE;
	if (pmt1->subtype != pmt2->subtype) return FALSE;
	if (pmt1->formattype != pmt2->formattype) return FALSE;

	if (pmt1->cbFormat != pmt2->cbFormat) return FALSE;
	if (pmt1->cbFormat)
	{
		if (memcmp(pmt1->pbFormat, pmt2->pbFormat, pmt1->cbFormat)) return FALSE;
	}
	return TRUE;
}
BOOL CMiiShowPinIn::Check(const CMediaType* pmt)
{
	if (pmt->majortype != MEDIATYPE_Video) return FALSE;
	if (pmt->subtype != MEDIASUBTYPE_YUY2) return FALSE;
	//if(pmt->subtype != MEDIASUBTYPE_YV12) return FALSE;
    //if(pmt->subtype != MEDIASUBTYPE_RGB24) return FALSE;
	if (pmt->formattype != FORMAT_VideoInfo) return FALSE;
	return TRUE;
}
HRESULT CMiiShowPinIn::CheckMediaType(const CMediaType* pmt)
{
	if(Check(pmt))
	{
		if(!m_tType.IsValid()) return S_OK;
		if(IsMatch(pmt, &m_tType)) return S_OK;
	}
	return S_FALSE;
}

HRESULT CMiiShowPinIn::GetMediaType(int iPosition, CMediaType *pMediaType)
{
	if (iPosition < 0)
	{
		return E_INVALIDARG;
	}

	if (!m_tType.IsValid())
	{
		return VFW_S_NO_MORE_ITEMS;
	}
	
	if (iPosition > 0)
	{
		return VFW_S_NO_MORE_ITEMS;
	}

	if (pMediaType)
	{
		CopyMediaType(pMediaType, &m_tType);
	}

	return S_OK;
}

CMediaType *CMiiShowPinIn::MediaType()
{
	return &m_mt;
}

extern void miidx_input_video(Int8 *pBuffer, UInt32 pts, Int32 width, Int32 height, Int32 size);

STDMETHODIMP CMiiShowPinIn::Receive(IMediaSample *pSample)
{
	HRESULT hr;
	char *pBuffer;
	int nLength;
	int width, height;
	REFERENCE_TIME tstart, tend;
		
	CheckPointer(pSample,E_POINTER);

	hr = pSample->GetPointer((BYTE **)&pBuffer);
	nLength = pSample->GetActualDataLength();

	hr = pSample->GetTime(&tstart, &tend);

	if(hr==S_OK)
	{
		mTimeStamp = tstart/10000;
		Int32 pts = mTimeStamp;//imptimer_get_msticks(0);
		//miidx_input_video(pBuffer, mTimeStamp, mOutWidth, mOutHeight);
		miidx_input_video(pBuffer, mTimeStamp, mOutWidth, mOutHeight, 0);
	}
	return S_OK;
}

STDMETHODIMP CMiiShowPinIn::EndOfStream(void)
{
	return S_OK;
}
